package com.hrms.controller.admin;

import com.hrms.entity.Orders;
import com.hrms.http.ResponseResult;
import com.hrms.service.OrdersService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/admin/history")
public class AdminHistoryController {
    // 管理员订单管理

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/init")
    public ResponseResult<List<Map<String, Object>>> init() {
        List<Map<String, Object>> list = ordersService.selectAll();
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/delete")
    public ResponseResult<Object> delete(@RequestBody EntityUtil entity) {
        // 删除历史订单
        Orders orders = new Orders();
        orders.setOrder_id(entity.getOrder_id());
        if(!ordersService.delete(orders)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
